package com.darrenchan.leetcode;

/**
 * @Desc 同向双指针
 * @Author chenchi03
 * @CreateTime 2019-12-24 11:37
 */
public class Q209 {
    public int minSubArrayLen(int s, int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int i = 0;
        int min = Integer.MAX_VALUE;
        int sum = 0;
        for (int j = 0; j < nums.length; j++) {
            sum += nums[j];
            while (sum >= s) {
                min = Math.min(j - i + 1, min);
                sum -= nums[i];
                i++;
            }
        }

        return min == Integer.MAX_VALUE ? 0 : min;
    }

    public static void main(String[] args) {
        System.out.println(new Q209().minSubArrayLen(7, new int[]{2, 3, 1, 2, 4, 3}));
    }
}
